﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using MaxStudio.Concrete.DBUtility; 

namespace MaxStudio.Concrete.Server.Dao
{
    public partial class ConcreteDaoDataContext : System.Data.Linq.DataContext
    {
        #region Product

        public List<tb_Product> GetAllProduct()
        {

            var query = (from obj in this.tb_Products
                         where obj.IsDelete == 0
                         select obj).ToList();
            return query;
        }

        public tb_Product GetProductById(int id)
        {
            return this.tb_Products.SingleOrDefault(tb_Product => tb_Product.Id == id);
        }

        public List<tb_Product> GetProductByCategoryId(int categoryId)
        {
            var query = (from obj in this.tb_Products
                         where obj.CategoryId == categoryId
                         select obj).ToList();
            return query;
        }



        public void CreateProduct(tb_Product entity)
        {
            entity.CreateDate = DateTime.Now;
            entity.CreateBy = 1;
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            tb_Products.InsertOnSubmit(entity);
            this.SubmitChanges();

        }

        public void ModifyProduct(tb_Product entity)
        {
            tb_Product instance = this.tb_Products.SingleOrDefault(p => p.Id == entity.Id);

            instance.IsDelete = entity.IsDelete;
            instance.ModifyBy = 1; //Need to get current user
            instance.ModifyDate = DateTime.Now;
            instance.Name = entity.Name;

            this.SubmitChanges();
        }
        public void DeleteProduct(tb_Product entity)
        {
            tb_Product deleteEntity = tb_Products.SingleOrDefault(p => p.Id == entity.Id);
            tb_Products.DeleteOnSubmit(deleteEntity);
            this.SubmitChanges();
        }

        #endregion

        #region Mapping with RM
        public void CreateMappingWithRM(tb_Mapping_RMProduct entity)
        { 
            tb_Mapping_RMProducts.InsertOnSubmit(entity);
            this.SubmitChanges(); 
        }

        public void DeleteMappingWithRM(int productId)
        {
            var query = (from obj in this.tb_Mapping_RMProducts
                         where obj.ProductId == productId
                         select obj).ToList();
            foreach (tb_Mapping_RMProduct deleteEntity in query)
            {
                tb_Mapping_RMProducts.DeleteOnSubmit(deleteEntity);
                this.SubmitChanges();
            }
        }

        public List<tb_Mapping_RMProduct> GetProductRMMappingByProductId(int productId)
        {
            var query = (from obj in this.tb_Mapping_RMProducts
                         where obj.ProductId == productId
                         select obj).ToList();
            return query;
        }
        

        #endregion
    }

}
